﻿using System;
using System.Collections.Generic;
using Base;
using UnityEngine;

namespace FSM
{
    public sealed class FsmComponent : GameFrameworkComponent
    {
        private IFsmManager m_FsmManager = null;

        /// <summary>
        /// 获取有限状态机的数量
        /// </summary>
        public int Count
        {
            get { return m_FsmManager.Count; }
        }

        /// <summary>
        /// 组件初始化
        /// </summary>
        protected override void Awake()
        {
            base.Awake();
            // m_FsmManager = IFsmManager;
            if (m_FsmManager == null)
                return;

        }

        /// <summary>
        /// 检查是否存在有限状态机
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public bool HasFsm<T>() where T : class
        {
            return m_FsmManager.HasFsm<T>();
        }

        /// <summary>
        /// 检查是否存在有限状态机
        /// </summary>
        /// <param name="ownerType"></param>
        /// <returns></returns>
        public bool HasFsm(Type ownerType)
        {
            return m_FsmManager.HasFsm(ownerType);
        }

        /// <summary>
        /// 检查是否存在有限状态机
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasFsm<T>(string name) where T : class
        {
            return m_FsmManager.HasFsm<T>(name);
        }

        /// <summary>
        /// 检查是否存在有限状态机
        /// </summary>
        /// <param name="ownerType"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasFsm(Type ownerType, string name)
        {
            return m_FsmManager.HasFsm(ownerType, name);
        }
        
        /// <summary>
        /// 获取有限状态机
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IFsm<T> GetFsm<T>() where T : class
        {
            return m_FsmManager.GetFsm<T>();
        }
        
        /// <summary>
        /// 获取有限状态机
        /// </summary>
        /// <param name="name"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IFsm<T> GetFsm<T>(string name) where T : class
        {
            return m_FsmManager.GetFsm<T>(name);
        }
        
        /// <summary>
        /// 获取有限状态机
        /// </summary>
        /// <param name="ownerType"></param>
        /// <returns></returns>
        public FsmBase GetFsm(Type ownerType)
        {
            return m_FsmManager.GetFsm(ownerType);
        }
        
        /// <summary>
        /// 获取有限状态机
        /// </summary>
        /// <param name="ownerType"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public FsmBase GetFsm(Type ownerType, string name)
        {
            return m_FsmManager.GetFsm(ownerType, name);
        }
        
        /// <summary>
        /// 创建有限状态机
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="owner"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public IFsm<T> CreateFsm<T>(T owner, params FsmState<T>[] states) where T : class
        {
            return m_FsmManager.CreateFsm<T>(owner, states);
        }

        /// <summary>
        /// 创建有限状态机
        /// </summary>
        /// <param name="name"></param>
        /// <param name="owner"></param>
        /// <param name="states"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) where T : class
        {
            return m_FsmManager.CreateFsm<T>(name, owner, states);
        }
        
        /// <summary>
        /// 创建有限状态机
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="states"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IFsm<T> CreateFsm<T>(T owner, List<FsmState<T>> states) where T : class
        {
            return m_FsmManager.CreateFsm<T>(owner, states);
        }
        
        /// <summary>
        /// 创建有限状态机
        /// </summary>
        /// <param name="name"></param>
        /// <param name="owner"></param>
        /// <param name="states"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IFsm<T> CreateFsm<T>(string name, T owner, List<FsmState<T>> states) where T : class
        {
            return m_FsmManager.CreateFsm<T>(name, owner, states);
        }

        /// <summary>
        /// 销毁有限状态机
        /// </summary>
        /// <typeparam name="T">有限状态机持有者类型</typeparam>
        public void DestroyFsm<T>() where T : class
        {
            m_FsmManager.DestroyFsm<T>();
        }
        
        public void DestroyFsm(Type ownerType)
        {
            m_FsmManager.DestroyFsm(ownerType);
        }
        
        public void DestroyFsm<T>(string name) where T : class
        {
            m_FsmManager.DestroyFsm<T>(name);
        }
        
        public void DestroyFsm(Type ownerType, string name)
        {
            m_FsmManager.DestroyFsm(ownerType, name);
        }

        public void DestroyFsm<T>(IFsm<T> fsm) where T : class
        {
            m_FsmManager.DestroyFsm(fsm);
        }

        public void DestroyFsm(FsmBase fsm)
        {
            m_FsmManager.DestroyFsm(fsm);
        }

    }
}